﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using V5_Apps.Core;
using WeifenLuo.WinFormsUI.Docking;

namespace V5_Apps.Plugins.模拟网页访问 {
    public partial class frmMain : DockContent, IApp {
        public frmMain() {
            InitializeComponent();
        }

        public ToolStripMenuItem[] GetMenuItem() {
            List<ToolStripMenuItem> list = new List<ToolStripMenuItem>();
            ToolStripMenuItem ts1 = new ToolStripMenuItem() { Text = "工具1" };
            ts1.Click += delegate(object sender, EventArgs e) {
                MessageBox.Show("工具1");
            };
            list.Add(ts1);

            ToolStripMenuItem t2 = new ToolStripMenuItem() { Text = "工具2" };
            t2.Click += delegate(object sender, EventArgs e) {
                MessageBox.Show("工具2");
            };
            list.Add(t2);

            return list.ToArray();
        }


        public ToolStripItem[] GetToolBarItem() {
            throw new NotImplementedException();
        }

        public ToolStripItem[] GetStatusItem() {
            throw new NotImplementedException();
        }

        private void btnSend_Click(object sender, EventArgs e) {
            string resultFlag = string.Empty;
            string postDataStr = this.txtPostData.Text;
            string url = this.txtUrl.Text;
            string endcode = this.cbbEncode.Text;
            if (string.IsNullOrEmpty(endcode)) {
                endcode = "utf-8";
            }
            Encoding encoding = Encoding.GetEncoding(endcode);
            byte[] postData = encoding.GetBytes(postDataStr);

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "POST";
            myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
            myRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            //myRequest.ContentType = "text/xml; charset=utf-8";
            myRequest.ContentLength = postData.Length;

            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();

            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            if (myResponse.StatusCode == HttpStatusCode.OK) {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding);
                resultFlag = reader.ReadToEnd();
            }

            this.txtLogView.Text = resultFlag;
        }
    }
}
